$(document).ready(function () {
    $(".dataTables-example").dataTable();
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "progressBar": true,
        "positionClass": "toast-top-right",
        "onclick": null,
        "showDuration": "400",
        "hideDuration": "1000",
        "timeOut": "7000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }

});

/**
 * 保存一个颜色
 */
function saveColor() {
    let colorName = $("#color-name").val();
    if (!colorName){
        toastr.error('颜色名称不能为空！','提示：')
        return ;
    }
    $.post("/admin/color/save", {
        colDesc: colorName
    }, function (resp) {
        if (resp.data) {
            toastr.success(resp.message, '提示');
            $("#myModal").modal('hide');
            setTimeout(function (){
                location.reload()
            },2000)
        } else {
            toastr.error(resp.message, '提示');
        }
    })
}

/**
 * 显示一个颜色
 * @param colId 颜色Id
 */
function showColor(colId) {
    $.get("/admin/color/getOne", {
        colId: colId
    }, function (resp) {
        console.log(resp)
        $("#color-id-update").val(resp.data.colId);
        $("#color-name-update").val(resp.data.colDesc);
    })
}

/**
 * 更新一个颜色
 */
function updateColor() {
    let colId = $("#color-id-update").val();
    let colDesc = $("#color-name-update").val();
    if (!colDesc){
        toastr.error('颜色名称不能为空！','提示：')
        return ;
    }
    $.post("/admin/color/update", {
        colId: colId,
        colDesc: colDesc
    }, function (resp) {
        if (resp.data) {
            toastr.success(resp.message, '提示');
            $("#updateModal").modal('hide');
            setTimeout(function (){
                location.reload()
            },2000)
        } else {
            toastr.error(resp.message, '提示');
        }
    })
}
function deleteOneColor(colId){
    swal({
        title:"是否删除?",
        text:"此行为影响已经保存的商品颜色信息！",
        type:"warning",
        //是否显示取消按钮
        showCancelButton:true,
        //按钮内容
        cancelButtonText:'取 消',
        cancelButtonColor:'#b9b9b9',
        showConfirmButton:true,
        confirmButtonText:'确 认',
        confirmButtonColor:"#dd6b55",
        //点击返回上一步操作
        closeOnConfirm:false,
        closeOnCancel:true
    },function(){
        //正确按钮进行的操作点
        $.get("/admin/color/delete",{
            colorId:colId
        },function (resp){
            if (resp.data){
                toastr.success(resp.message,'提示')
                setTimeout(function () {
                    window.location.reload();
                },2000)
            }else{
                toastr.error(resp.message,'提示')
            }
        })
    });
}
//abandon
function chooseColor4Product() {
    //批量删除
    let selects = [];
    let proId = $("#hidden-pro-id").val();
    let count = 0;
    $("input[name='inp-check-box']:checked").each(function () {
        if ($(this).prop('checked')) {
            //如果当前属性为checked
            selects[count++] = $(this).val();
        }
    });
    if (selects.length === 0) {
        swal("您没有选中任何数据，请选中后重试！");
    }
    $.post("/admin/product/saveColor4Product", {
        selects: selects,
        proId: proId
    }, function (resp) {
        if (resp.data) {
            toastr.success(resp.message, "提示");
        } else {
            toastr.error(resp.message, '提示')
        }
    })
}
